package com.zhnc.dangjianpingtaiapp.gui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wangdh.utilslibrary.utils.logger.TLog;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.argum.FragmentTitleRely;

/**
 *
 */
public class BaseTitleFragment extends Fragment {
    private String title;
    private String more;
    private View.OnClickListener moreClick;

    public BaseTitleFragment() {
    }

    private FragmentTitleRely rely;

    public void setTitleRely(Object msg) {
        if (msg instanceof FragmentTitleRely) {
            rely = (FragmentTitleRely) msg;
        } else {
            return;
        }
        TLog.e("title 依赖数据 获取");
        isShowTitle = rely.isShowTitle();
        title = rely.getTitleName();
        more = rely.getMore();
        moreClick = rely.getMoreClick();
    }
//    public static BaseTitleFragment newInstance(String param1, String param2) {
//        BaseTitleFragment fragment = new BaseTitleFragment();
//        Bundle args = new Bundle();
//        args.putString(key_Name, param1);
//        args.putString(key_More, param2);
//        fragment.setArguments(args);
//        return fragment;
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            title = getArguments().getString(key_Name);
//            more = getArguments().getString(key_More);
//        }
    }

    private LayoutInflater mInflater;
    private boolean isShowTitle = true;
    private boolean isShowLine = true;

    private View rl_title;
    private TextView title_name;
    private TextView title_more;
    private View line;

    public Fragment setShowTitle(boolean bool) {
        this.isShowTitle = bool;
        return this;
    }

    public void setTitleName(String msg) {
        this.title = msg;
        updateTitle();
    }

    public void setTitleMore(String msg) {
        this.more = msg;
        updateTitle();
    }

    public void updateTitle() {
        title_name.setText(this.title);
        title_more.setText(this.more);
    }

    public void setMoreVisibility(int flag) {
        title_more.setVisibility(flag);
    }

    public void setTitleVisibility(int flag) {
        rl_title.setVisibility(flag);
        line.setVisibility(flag);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.mInflater = inflater;
        View inflate = inflater.inflate(R.layout.fragment_bast_title, container, false);

        rl_title = inflate.findViewById(R.id.rl_title);
        title_name = inflate.findViewById(R.id.title_name);
        title_more = inflate.findViewById(R.id.title_more);
        line = inflate.findViewById(R.id.line);

        if (isShowTitle) {
            setTitleVisibility(View.VISIBLE);
            title_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TLog.e("点击右边");
                    moreClick.onClick(v);
                }
            });
            title_name.setText(title);
            title_more.setText(more);

        } else {
            setTitleVisibility(View.GONE);
        }
        return inflate;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FrameLayout ll_title_fragment_content = super.getView().findViewById(R.id.ll_title_fragment_content);
        if (this.contentView != null) {
            ll_title_fragment_content.removeAllViews();
            TLog.e("添加子view" + contentView.toString());
            ll_title_fragment_content.addView(this.contentView);
        }
    }

    private View contentView;

    @Nullable
    @Override
    public View getView() {
        return contentView;
    }

    public void setContentView(View msg) {
        this.contentView = msg;
    }

    public void setContentView(int msg) {
        this.contentView = View.inflate(this.getActivity(), msg, null);
//        this.contentView = this.mInflater.inflate(msg, null, false);
    }


}
